package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.WmsInPlan;
import com.ruoyi.business.domain.WmsInWarehous;
import com.ruoyi.business.domain.WmsInWarehousDetail;
import com.ruoyi.business.domain.WmsTransfer;
import com.ruoyi.business.mapper.WmsInPlanMapper;
import com.ruoyi.business.mapper.WmsInWarehousDetailMapper;
import com.ruoyi.business.mapper.WmsInWarehousMapper;
import com.ruoyi.business.service.IWmsInWarehousDetailService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * 入库单明细Service业务层处理
 * 
 * @author sz
 * @date 2024-01-08
 */
@Service
public class WmsInWarehousDetailServiceImpl implements IWmsInWarehousDetailService
{
    @Autowired
    private WmsInWarehousDetailMapper wmsInWarehousDetailMapper;
    @Autowired
    private WmsInWarehousMapper wmsInWarehousMapper;
    @Autowired
    private WmsInPlanMapper wmsInPlanMapper;

    /**
     * 查询入库单明细
     * 
     * @param id 入库单明细ID
     * @return 入库单明细
     */
    @Override
    public WmsInWarehousDetail selectWmsInWarehousDetailById(Long id)
    {
        return wmsInWarehousDetailMapper.selectWmsInWarehousDetailById(id);
    }

    @Override
    public WmsInWarehousDetail selectWmsInWarehousDetailBySn(WmsInWarehousDetail info) {
        return wmsInWarehousDetailMapper.selectWmsInWarehousDetailBySn(info);
    }

    /**
     * 查询入库单明细列表
     * 
     * @param wmsInWarehousDetail 入库单明细
     * @return 入库单明细
     */
    @Override
    public List<WmsInWarehousDetail> selectWmsInWarehousDetailList(WmsInWarehousDetail wmsInWarehousDetail)
    {
        List<WmsInWarehousDetail> list = wmsInWarehousDetailMapper.selectWmsInWarehousDetailList(wmsInWarehousDetail);
        return list;
    }

    /**
     * 新增入库单明细
     * 
     * @param wmsInWarehousDetail 入库单明细
     * @return 结果
     */
    @Override
    public int insertWmsInWarehousDetail(WmsInWarehousDetail wmsInWarehousDetail)
    {
        wmsInWarehousDetail.setCreateTime(DateUtils.getNowDate());
        if(wmsInWarehousDetail.getType() == 1){
            wmsInWarehousDetail.setProcessStatus(1);
        }else{
            wmsInWarehousDetail.setProcessStatus(0);
        }
        wmsInWarehousDetailMapper.insertWmsInWarehousDetail(wmsInWarehousDetail);

        if(wmsInWarehousDetail.getType() == 1){
            //计算入库单及入库计划的状态
            Integer status = getStatus(wmsInWarehousDetail.getInWarehousId());
            WmsInWarehous wmsInWarehous = new WmsInWarehous();
            wmsInWarehous.setId(wmsInWarehousDetail.getInWarehousId());
            wmsInWarehous.setStatus(status);
            wmsInWarehousMapper.updateWmsInWarehous(wmsInWarehous);
            WmsInWarehous inWarehous = wmsInWarehousMapper.selectWmsInWarehousById(wmsInWarehousDetail.getInWarehousId());
            //更新入库计划状态
            WmsInPlan wmsInPlan = new WmsInPlan();
            wmsInPlan.setId(inWarehous.getInPlanId());
            wmsInPlan.setStatus(status);
            wmsInPlanMapper.updateWmsInPlan(wmsInPlan);
        }
        return 1;
    }

    /**
     * 根据入库单id，实时统计入库状态
     * @param inWarehousId
     * @return
     */
    public int getStatus(Long inWarehousId){
        Integer status = 1;
        //根据入库单id，统计入库良品数量
        Long inStoreNum = wmsInWarehousDetailMapper.statNum(inWarehousId);
        if(inStoreNum == null){
            inStoreNum = 0L;
        }
        //根据入库单id,统计入库计划数量
        WmsInWarehous obj = wmsInWarehousMapper.selectWmsInWarehousById(inWarehousId);
        Long inPlanNum = wmsInPlanMapper.statNum(obj.getInPlanId());
        if(inPlanNum == null){
            inPlanNum = 0L;
        }

        if(inStoreNum == 0L && inPlanNum == 0L){
            status = 1;
        }else{
            if(inStoreNum >= inPlanNum){
                status =2;
            }else if(inStoreNum < inPlanNum && inStoreNum > 0){
                status =3;
            }else {
                status = 1;
            }
        }
        return status;
    }



    /**
     * 修改入库单明细
     * 
     * @param info 入库单明细
     * @return 结果
     */
    @Override
    public int updateWmsInWarehousDetail(WmsInWarehousDetail info)
    {
        info.setUpdateTime(DateUtils.getNowDate());
        wmsInWarehousDetailMapper.updateWmsInWarehousDetail(info);

        WmsInWarehousDetail wmsInWarehousDetail = wmsInWarehousDetailMapper.selectWmsInWarehousDetailById(info.getId());

        //计算入库单及入库计划的状态
        Integer status = getStatus(wmsInWarehousDetail.getInWarehousId());
        WmsInWarehous wmsInWarehous = new WmsInWarehous();
        wmsInWarehous.setId(wmsInWarehousDetail.getInWarehousId());
        wmsInWarehous.setStatus(status);
        wmsInWarehousMapper.updateWmsInWarehous(wmsInWarehous);
        WmsInWarehous inWarehous = wmsInWarehousMapper.selectWmsInWarehousById(wmsInWarehousDetail.getInWarehousId());
        //更新入库计划状态
        WmsInPlan wmsInPlan = new WmsInPlan();
        wmsInPlan.setId(inWarehous.getInPlanId());
        wmsInPlan.setStatus(status);
        wmsInPlanMapper.updateWmsInPlan(wmsInPlan);
        return 1;
    }

    /**
     * 删除入库单明细对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWmsInWarehousDetailByIds(String ids)
    {
        return wmsInWarehousDetailMapper.deleteWmsInWarehousDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除入库单明细信息
     * 
     * @param id 入库单明细ID
     * @return 结果
     */
    @Override
    public int deleteWmsInWarehousDetailById(Long id)
    {
        return wmsInWarehousDetailMapper.deleteWmsInWarehousDetailById(id);
    }

    /**
     * 审核
     */
    @Override
    public void processData(WmsInWarehousDetail info) {
        if(info.getProcessStatus() == 1){
            //审核通过
            wmsInWarehousDetailMapper.updateWmsInWarehousDetail(info);

            WmsInWarehousDetail wmsInWarehousDetail = wmsInWarehousDetailMapper.selectWmsInWarehousDetailById(info.getId());
            //计算入库单及入库计划的状态
            Integer status = getStatus(wmsInWarehousDetail.getInWarehousId());
            WmsInWarehous wmsInWarehous = new WmsInWarehous();
            wmsInWarehous.setId(wmsInWarehousDetail.getInWarehousId());
            wmsInWarehous.setStatus(status);
            wmsInWarehousMapper.updateWmsInWarehous(wmsInWarehous);
            WmsInWarehous inWarehous = wmsInWarehousMapper.selectWmsInWarehousById(wmsInWarehousDetail.getInWarehousId());
            //更新入库计划状态
            WmsInPlan wmsInPlan = new WmsInPlan();
            wmsInPlan.setId(inWarehous.getInPlanId());
            wmsInPlan.setStatus(status);
            wmsInPlanMapper.updateWmsInPlan(wmsInPlan);
        }else{
            wmsInWarehousDetailMapper.updateWmsInWarehousDetail(info);
        }
    }

    /**
     * 查询入库单里面可调拨的商品列表
     * @param info
     * @return
     */
    @Override
    public List<WmsInWarehousDetail> queryGoodList(WmsTransfer info) {
        return wmsInWarehousDetailMapper.queryGoodList(info);
    }

    /**
     * 查询过期预警列表
     * @param info
     * @return
     */
    @Override
    public List<WmsInWarehousDetail> queryValidityList(WmsInWarehousDetail info) {
        return wmsInWarehousDetailMapper.queryValidityList(info);
    }

    @Override
    public List<WmsInWarehousDetail> queryStoreList(WmsInWarehousDetail info) {
        return wmsInWarehousDetailMapper.queryStoreList(info);
    }

    /**
     * 根据货物编码查询货物的所有批次号
     * @param cargoSn
     * @return
     */
    @Override
    public List<Map<String, Object>> queryCargoList(String cargoSn) {
        return wmsInWarehousDetailMapper.queryCargoList(cargoSn);
    }
}
